package com.peakmain.wanandroid.mymodule.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.peakmain.baselibrary.recyclerview.listener.OnItemClickListener;
import com.peakmain.minemodule.R;
import com.peakmain.wanandroid.mymodule.adapter.GankImageAdapter;
import com.peakmain.wanandroid.mymodule.base.BaseMineActivity;
import com.peakmain.wanandroid.mymodule.bean.Constant;
import com.peakmain.wanandroid.mymodule.contract.GankContract;
import com.peakmain.wanandroid.mymodule.presenter.GankPresenter;
import com.peakmain.wanandroid.netmodule.bean.gank.GankImageBean;

import java.util.ArrayList;
import java.util.List;

/**
 * author: peakmain
 * createdata：2019/7/26
 * mail: 2726449200@qq.com
 * desiption:
 */
public class GankActivity extends BaseMineActivity<GankPresenter> implements GankContract.View {
    public RecyclerView mRecyclerCard;
    private List<GankImageBean> mBean;
    private GankImageAdapter mAdapter;
    private int pageNum = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_gank;
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected void initView() {
        mRecyclerCard = findViewById(R.id.recycler_card);
        mPresenter.getGankImageData("福利", 20);

    }

    @Override
    public void showGankImageSuccess(final List<GankImageBean> imageBeans) {
        //ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new CardItemTouchCallBack(mCardItemHelper));
        mRecyclerCard.setLayoutManager(new GridLayoutManager(this, 2));
        mRecyclerCard.setHasFixedSize(true);
        //itemTouchHelper.attachToRecyclerView(mRecyclerCard);
        mAdapter = new GankImageAdapter(this, imageBeans);
        mRecyclerCard.setAdapter(mAdapter);
        final ArrayList<String> mImageUrl = new ArrayList<>();
        for (GankImageBean imageBean : imageBeans) {
            mImageUrl.add(imageBean.getUrl());
        }
        setOnClick(mImageUrl);
        this.mBean = imageBeans;
    }

    private void setOnClick(final ArrayList<String> mImageUrl) {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Intent intent = new Intent(GankActivity.this, ImagePreviewActivity.class);
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.POSITION, position);
                bundle.putSerializable(Constant.DATA,mImageUrl);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    /*private CardItemHelper mCardItemHelper = new CardItemHelper() {
        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            if (mBean != null) {
                mBean.remove(viewHolder.getAdapterPosition());
                mAdapter.notifyDataSetChanged();
                if (mBean.size() == 0) {
                    pageNum++;
                    mPresenter.getGankImageData("福利", 10, pageNum);
                }

            }
        }
    };*/
}
